package util;

import com.alibaba.fastjson.JSONObject;

/**
 * @Author: 廖卓斯
 * @CreateTime: 2024-12-25
 * @Description: 代码返回类
 * @Version: 1.0
 */

public class Result {
    // 响应的状态码
    private int code;
    // 响应的消息
    private String message;
    // 响应的数据
    private Object data;
    private long runTime;
    private long compileTime;

    // 私有构造函数，防止外部直接实例化
    private Result(int code, String message, Object data, long runTime, long compileTime) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.runTime = runTime;
        this.compileTime = compileTime;
    }

    public long getRunTime() {
        return runTime;
    }

    public long getCompileTime() {
        return compileTime;
    }

    // 获取状态码
    public int getCode() {
        return code;
    }

    // 获取消息
    public String getMessage() {
        return message;
    }

    // 获取数据
    public Object getData() {
        return data;
    }

    // 构建成功的响应
    public static Result success(Object data, long runTime, long compileTime) {
        return new Result(200, "成功", data, runTime, compileTime);
    }

    // 构建失败的响应
    public static Result error(String errorCode, String errorMessage) {
        return new Result(Integer.parseInt(errorCode), errorMessage, null, -1, -1);
    }

    // 为了方便调试和日志记录，重写toString方法
    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
